package com.alinesno.cloud.initializr.starter.gateway.rest;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alinesno.cloud.base.boot.entity.ManagerAccountEntity;
import com.alinesno.cloud.common.core.constants.SpringInstanceScope;
import com.alinesno.cloud.common.facade.constants.FieldConstants;
import com.alinesno.cloud.common.facade.pageable.DatatablesPageBean;
import com.alinesno.cloud.common.facade.response.AjaxResult;
import com.alinesno.cloud.initializr.starter.entity.BuildGitEntity;
import com.alinesno.cloud.initializr.starter.gateway.aop.DataFilter;
import com.alinesno.cloud.initializr.starter.init.InitDataService;
import com.alinesno.cloud.initializr.starter.service.IBuildGitService;
import com.alinesno.cloud.rest.gateway.adapter.page.TableDataInfo;
import com.alinesno.cloud.rest.gateway.adapter.plugins.TranslateCode;
import com.alinesno.cloud.rest.gateway.adapter.rest.BaseRestAdapter;
import com.alinesno.cloud.rest.gateway.adapter.security.CurrentAccountJwt;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * 【请填写功能名称】Controller
 *
 * @author alinesno
 * @date 2021-06-05
 */
@RestController
@Scope(SpringInstanceScope.PROTOTYPE)
@RequestMapping("com/alinesno/cloud/business/buildGit")
public class BuildGitRest extends BaseRestAdapter<BuildGitEntity, IBuildGitService> {

	// 日志记录
	private static final Logger log = LoggerFactory.getLogger(BuildGitRest.class);

	@Autowired
	private IBuildGitService buildGitService;

	@Autowired
	private InitDataService initDataService;

	@TranslateCode
	@ResponseBody
	@DataFilter
	@PostMapping("/datatables")
	public TableDataInfo datatables(HttpServletRequest request, Model model, DatatablesPageBean page) {
		log.debug("page = {}", ToStringBuilder.reflectionToString(page));

		String userId = CurrentAccountJwt.getUserId();
		QueryWrapper<BuildGitEntity> wrapper = new QueryWrapper<BuildGitEntity>();
		wrapper.eq(FieldConstants.OPERATOR_ID, userId);
		int countGitRepository = buildGitService.count(wrapper);

		// 初始化用户仓库
		if (countGitRepository == 0) {
			initDataService.initAccountGitRepository(CurrentAccountJwt.getUserId());
		}

		TableDataInfo tableInfo = this.toPage(model, this.getFeign(), page);

		return tableInfo;
	}

	@Override
	public IBuildGitService getFeign() {
		return this.buildGitService;
	}

	@ResponseBody
	@PostMapping("saveEntity")
	public AjaxResult save(Model model, @RequestBody BuildGitEntity entity) {

		log.debug("===> save Entity:{}", ToStringBuilder.reflectionToString(entity));

		if (entity.getGitType().equals("gitlab")) {
			entity.setGitIcon("fab fa-gitlab");
		} else if (entity.getGitType().equals("gitee")) {
			entity.setGitIcon("fab fa-gitkraken");
		} else if (entity.getGitType().equals("github")) {
			entity.setGitIcon("fab fa-github");
		}

		ManagerAccountEntity e = CurrentAccountJwt.get();

		if (e != null) {
			entity.setOperatorId(e.getId());
			entity.setTenantId(e.getTenantId());
			entity.setDepartmentId(e.getDepartmentId());
		}

		getFeign().save(entity);

		return AjaxResult.success();
	}

	/**
	 * 绑定gitlab账号
	 * 
	 * @param gitType
	 * @return
	 */
	@GetMapping("bindingGit")
	public AjaxResult bindingGit(String gitType) {
		
		log.debug("git type = {}" , gitType);

		return AjaxResult.success();
	}

}
